package com.af.example;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author : zhenyun.su
 * @since : 2023/4/20
 */

public class AfStreamDemo {

    public static void main(String[] args) {
        List<Use> useList1 = new ArrayList();
        List<Use> useList = Arrays.asList(
                new Use(1L, "su", 20),
                new Use(2L, "zhen", 30),
                new Use(3L, "yun", 40)
        );

        // [] 不会报异常， 只是返回空值的数组
        List<String> test=  useList1.stream().map(item-> item.getName()).collect(Collectors.toList());
        // [su, zhen, yun]
        List<String> names=  useList.stream().map(item-> item.getName()).collect(Collectors.toList());

        List<UseDto> useDtos=  useList.stream().map(item-> new UseDto(item.getId(), item.getName())).collect(Collectors.toList());
        List<Map<String, Object>> maps = new ArrayList<>();

        Map<Long, Use> newUsers = useList.stream().collect(Collectors.toMap(Use::getId, Function.identity(), (key1, key2) -> key2));

        //{1=su, 2=zhen, 3=yun}
        Map<Long, String> map=  useList.stream().collect(Collectors.toMap(Use::getId,Use::getName));
        System.out.println(names);
        System.out.println(map);

    }

    private static class Use{
        private Long id;
        private String name;
        private Integer age;

        public Use(Long id, String name, Integer age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }
    }

    private static class UseDto{
        private Long id;
        private String name;

        public UseDto(Long id, String name) {
            this.id = id;
            this.name = name;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
